using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Eudaemon.ProeAssist.WinGUI
{
    public struct ConfigInfo
    {
        public ConfigInfo(int gMainFormWidth, int gMainFormHeight,
            int gLeftPanelWidth, string gLastVisitPath, 
            bool gShowProeFilesOnly, bool gShowNewestVersionOnly)//, int[] gColumnWidths)
        {
            MainFormWidth = gMainFormWidth;
            MainFormHeight = gMainFormHeight;
            LeftPanelWidth = gLeftPanelWidth;
            LastVisitPath = gLastVisitPath;
            ShowProeFilesOnly = gShowProeFilesOnly;
            ShowNewestVersionOnly = gShowNewestVersionOnly;
            //ListColunmWidths = gColumnWidths;
        }

        // GUI: Windows Layout
        public int MainFormWidth;
        public int MainFormHeight;
        public int LeftPanelWidth;
        //public int[] ListColunmWidths;
        // Data Recently Visited
        public string LastVisitPath;
        public bool ShowProeFilesOnly;
        public bool ShowNewestVersionOnly;

    }

    class ConfigMan
    {
        private string _configPath =
            Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),"pa_cfg.xml");
        private ConfigInfo _info = new ConfigInfo(800, 600, 256,
            Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
            true, true);//, new int[6] {200, 100, 100, 100, 100, 100});
        private string _errMsg;
        public string ErrMsg
        {
            get
            {
                return _errMsg;
            }
        }

        /// <summary>
        /// Read the config file in if exsits 
        /// </summary>
        /// <returns></returns>
        public ConfigInfo LoadConfig()
        {
            if (File.Exists(_configPath))
            {
                ReadConfig(_configPath);
            }

            return _info;
        }

        /// <summary>
        /// Save configurations into the config file(newly created if not found)
        /// </summary>
        /// <param name="info">configurations structure</param>
        /// <returns>return true if successfully saved, false otherwise.</returns>
        public bool SaveConfig(ConfigInfo info)
        {
            _info = info;
            CreateConfig(_configPath);
            return true;
        }

        private void ReadConfig(string sFileName)
        {
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(sFileName);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "MainFormWidth":
                                _info.MainFormWidth = reader.ReadElementContentAsInt();
                                break;
                            case "MainFormHeight":
                                _info.MainFormHeight = reader.ReadElementContentAsInt();
                                break;
                            case "LeftPanelWidth":
                                _info.LeftPanelWidth = reader.ReadElementContentAsInt();
                                break;
                            case "LastVisitPath":
                                _info.LastVisitPath = reader.ReadElementContentAsString();
                                break;
                            case "ShowProeFilesOnly":
                                _info.ShowProeFilesOnly = reader.ReadElementContentAsBoolean();
                                break;
                            case "ShowNewestVersionOnly":
                                _info.ShowNewestVersionOnly = reader.ReadElementContentAsBoolean();
                                break;
                            //case "ListColumnWidths":
                            //    string temp = reader.ReadElementContentAsString();
                            //    string[] strWidths = temp.Split(new Char[]{',' , ',',  ';', '\t'});
                            //    _info.ListColunmWidths = new int[strWidths.Length];
                            //    for (int i = 0; i<strWidths.Length; ++i)
                            //    {
                            //        try
                            //        {
                            //            _info.ListColunmWidths[i] = Int32.Parse(strWidths[i]);
                            //        }
                            //        catch
                            //        {
                            //            _info.ListColunmWidths[i] = 100;
                            //        }
                            //    }
                            //    break;
                        }
                    }
                }
            }
            catch (XmlException xe)
            {
                _errMsg = "Failed to read " + sFileName + " because: \r\n" + xe.ToString();
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }


        private void CreateConfig(string sFileName)
        {
            XmlTextWriter writer = null;
            try
            {
                writer = new XmlTextWriter(sFileName, new UTF8Encoding());
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;

                writer.WriteStartDocument();
                writer.WriteStartElement("ProE_Assist");
                writer.WriteAttributeString("version", "1.0");

                writer.WriteStartElement("Layout");
                writer.WriteElementString("MainFormWidth", _info.MainFormWidth.ToString());
                writer.WriteElementString("MainFormHeight", _info.MainFormHeight.ToString());
                writer.WriteElementString("LeftPanelWidth", _info.LeftPanelWidth.ToString());
                writer.WriteElementString("ShowProeFilesOnly", _info.ShowProeFilesOnly.ToString().ToLower());
                writer.WriteElementString("ShowNewestVersionOnly", _info.ShowNewestVersionOnly.ToString().ToLower());
                //StringBuilder sb = new StringBuilder();
                //foreach (int width in _info.ListColunmWidths)
                //{
                //    sb.Append(width.ToString());
                //    sb.Append(" ");
                //}
                //writer.WriteElementString("ListColumnWidths", sb.ToString());
                writer.WriteEndElement();

                writer.WriteStartElement("Data");
                writer.WriteElementString("LastVisitPath", _info.LastVisitPath);
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
            }
            catch (XmlException xe)
            {
                _errMsg = "Failed to create " + sFileName + " because: \r\n" + xe.ToString();
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }

        private string FixXmlBoolRW(bool bVal)
        {
            int nVal = bVal ? 1 : 0;
            return nVal.ToString();
        } 
    }
}

